package com.se7en.ufutil;

import java.util.ArrayList;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.widget.Toast;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class Parking extends BalloonItemizedOverlay<OverlayItem> {

	private ArrayList<OverlayItem> m_overlays = new ArrayList<OverlayItem>();
	private Context ctx;
	private ParkingModel[] parkingLocs;
	DataAccess dataAccess;
	MapView mapView;
	static Drawable red1Marker, orangeMarker, gatedMarker, reservedMarker, carpoolMarker, greenMarker, scooterMarker, errorMarker;

	public Parking(Drawable defaultMarker, Context context, DataAccess dataAccess, MapView mapView) {
		super(boundCenter(defaultMarker), mapView);
		ctx = context;
		this.dataAccess = dataAccess;
		this.mapView = mapView;
		parkingLocs = dataAccess.getAllParking();
		red1Marker = ctx.getResources().getDrawable(R.drawable.red_parking);
		orangeMarker = ctx.getResources().getDrawable(R.drawable.orange_parking);
		gatedMarker = ctx.getResources().getDrawable(R.drawable.gated_parking);
		reservedMarker = ctx.getResources().getDrawable(R.drawable.reserved_parking);
		carpoolMarker = ctx.getResources().getDrawable(R.drawable.carpool_parking);
		greenMarker = ctx.getResources().getDrawable(R.drawable.green_parking);
		scooterMarker = ctx.getResources().getDrawable(R.drawable.scooter_parking);
		errorMarker = ctx.getResources().getDrawable(R.drawable.error);
		OverlayItem temp;

		if(parkingLocs.length > 1) {
			for(ParkingModel park : parkingLocs) {
				switch(park.type)
				{
					case RED_1:
						temp = new OverlayItem(park.location, "Red 1 Decal", "\t"+park.times+"\n\t\t"+park.days);
						temp.setMarker(red1Marker);
						red1Marker.setBounds(-red1Marker.getIntrinsicWidth()/2, -red1Marker.getIntrinsicHeight(), red1Marker.getIntrinsicWidth() /2, 0);
						break;
					case ORANGE:
						temp = new OverlayItem(park.location, "Orange Decal", "\t"+park.times+"\n\t\t"+park.days);
						temp.setMarker(orangeMarker);
						orangeMarker.setBounds(-orangeMarker.getIntrinsicWidth()/2, -orangeMarker.getIntrinsicHeight(), orangeMarker.getIntrinsicWidth() /2, 0);
						break;
					case GATED:
						temp = new OverlayItem(park.location, "Gated Decal", "\t"+park.times+"\n\t\t"+park.days);
						temp.setMarker(gatedMarker);
						gatedMarker.setBounds(-gatedMarker.getIntrinsicWidth()/2, -gatedMarker.getIntrinsicHeight(), gatedMarker.getIntrinsicWidth() /2, 0);
						break;
					case RESERVED:
						temp = new OverlayItem(park.location, "Reserved", "\t"+park.times+"\n\t\t"+park.days);
						temp.setMarker(reservedMarker);
						reservedMarker.setBounds(-reservedMarker.getIntrinsicWidth()/2, -reservedMarker.getIntrinsicHeight(), reservedMarker.getIntrinsicWidth() /2, 0);
						break;
					case CARPOOL:
						temp = new OverlayItem(park.location, "Carpool Decal", "\t"+park.times+"\n\t\t"+park.days);
						temp.setMarker(carpoolMarker);
						carpoolMarker.setBounds(-carpoolMarker.getIntrinsicWidth()/2, -carpoolMarker.getIntrinsicHeight(), carpoolMarker.getIntrinsicWidth() /2, 0);
						break;
					case GREEN:
						temp = new OverlayItem(park.location, "Green Decal", "\t"+park.times+"\n\t\t"+park.days);
						temp.setMarker(greenMarker);
						greenMarker.setBounds(-greenMarker.getIntrinsicWidth()/2, -greenMarker.getIntrinsicHeight(), greenMarker.getIntrinsicWidth() /2, 0);
						break;
					case SCOOTER:
						temp = new OverlayItem(park.location, "Scooter Decal", "\t"+park.times+"\n\t\t"+park.days);
						temp.setMarker(scooterMarker);
						scooterMarker.setBounds(-scooterMarker.getIntrinsicWidth()/2, -scooterMarker.getIntrinsicHeight(), scooterMarker.getIntrinsicWidth() /2, 0);
						break;
					default:
						temp = new OverlayItem(park.location, "Unknown", "\t"+park.times+"\n\t\t"+park.days);
						temp.setMarker(errorMarker);
						errorMarker.setBounds(-errorMarker.getIntrinsicWidth()/2, -errorMarker.getIntrinsicHeight(), errorMarker.getIntrinsicWidth() /2, 0);
						break;
				}
				this.addOverlay(temp);
			}
		}
	}

	public void addOverlay(OverlayItem overlay) {
	    m_overlays.add(overlay);
	    setLastFocusedIndex(-1);
	    populate();
	}

	public void resetOverlay(boolean drawCode[]) {
	   // int code[] = drawCode;
		m_overlays.clear();
	    if(parkingLocs.length > 1) {
			for(ParkingModel park : parkingLocs) {
				OverlayItem temp = null;
				boolean init = false;
				switch(park.type)
				{
					case RED_1:
						if(drawCode[4]){
							temp = new OverlayItem(park.location, "Red 1 Decal", "\t"+park.times+"\n\t\t"+park.days);
							temp.setMarker(red1Marker);
							red1Marker.setBounds(-red1Marker.getIntrinsicWidth()/2, -red1Marker.getIntrinsicHeight(), red1Marker.getIntrinsicWidth() /2, 0);
							init = true;
						}
						break;
					case ORANGE:
						if(drawCode[3]){
							temp = new OverlayItem(park.location, "Orange Decal", "\t"+park.times+"\n\t\t"+park.days);
							temp.setMarker(orangeMarker);
							orangeMarker.setBounds(-orangeMarker.getIntrinsicWidth()/2, -orangeMarker.getIntrinsicHeight(), orangeMarker.getIntrinsicWidth() /2, 0);
							init = true;
						}
						break;
					case GATED:
						if(drawCode[1]){
							temp = new OverlayItem(park.location, "Gated Decal", "\t"+park.times+"\n\t\t"+park.days);
							temp.setMarker(gatedMarker);
							gatedMarker.setBounds(-gatedMarker.getIntrinsicWidth()/2, -gatedMarker.getIntrinsicHeight(), gatedMarker.getIntrinsicWidth() /2, 0);
							init = true;
						}
						break;
					case RESERVED:
						if(drawCode[5]){
							temp = new OverlayItem(park.location, "Reserved", "\t"+park.times+"\n\t\t"+park.days);
							temp.setMarker(reservedMarker);
							reservedMarker.setBounds(-reservedMarker.getIntrinsicWidth()/2, -reservedMarker.getIntrinsicHeight(), reservedMarker.getIntrinsicWidth() /2, 0);
							init = true;
						}
						break;
					case CARPOOL:
						if(drawCode[0]){
							temp = new OverlayItem(park.location, "Carpool Decal", "\t"+park.times+"\n\t\t"+park.days);
							temp.setMarker(carpoolMarker);
							carpoolMarker.setBounds(-carpoolMarker.getIntrinsicWidth()/2, -carpoolMarker.getIntrinsicHeight(), carpoolMarker.getIntrinsicWidth() /2, 0);
							init = true;
						}
						break;
					case GREEN:
						if(drawCode[2]){
							temp = new OverlayItem(park.location, "Green Decal", "\t"+park.times+"\n\t\t"+park.days);
							temp.setMarker(greenMarker);
							greenMarker.setBounds(-greenMarker.getIntrinsicWidth()/2, -greenMarker.getIntrinsicHeight(), greenMarker.getIntrinsicWidth() /2, 0);
							init = true;
						}
						break;
					case SCOOTER:
						if(drawCode[6]){
							temp = new OverlayItem(park.location, "Scooter Decal", "\t"+park.times+"\n\t\t"+park.days);
							temp.setMarker(scooterMarker);
							scooterMarker.setBounds(-scooterMarker.getIntrinsicWidth()/2, -scooterMarker.getIntrinsicHeight(), scooterMarker.getIntrinsicWidth() /2, 0);
							init = true;
						}
						break;
					default:
						temp = new OverlayItem(park.location, "Unknown", "\t"+park.times+"\n\t\t"+park.days);
						temp.setMarker(errorMarker);
						errorMarker.setBounds(-errorMarker.getIntrinsicWidth()/2, -errorMarker.getIntrinsicHeight(), errorMarker.getIntrinsicWidth() /2, 0);
						init = true;
						break;
				}
				if(init == true)
					this.addOverlay(temp);
			}
			populate();
		}
	}

	@Override
	protected OverlayItem createItem(int i) {
		return m_overlays.get(i);
	}

	@Override
	public int size() {
		return m_overlays.size();
	}

	//Makin helpful toast notifications.
	@Override
	protected boolean onBalloonTap(int index, OverlayItem item) {
		Toast.makeText(ctx, "Press the close button to hide popup", Toast.LENGTH_LONG).show();
		return true;
	}

}
